import EventDispatcher from "./EventDispatcher";

export default class EventProxy {


    private dispatcher: EventDispatcher;
    private eventMap = {};
    constructor() {
    }
    setDispatcher(dispatcher: EventDispatcher) {
        this.dispatcher = dispatcher;
    }
    on(eventName, func: Function, target: Object) {
        if (!this.eventMap[eventName]) {
            this.dispatcher.on(eventName, func, target);
            this.eventMap[eventName] = { eventName: eventName, target: target, func: func }
        }
    }
    has(eventName) {
        return this.eventMap[eventName] != undefined && this.eventMap[eventName] != null;
    }
    offAll() {
        if (this.dispatcher) {
            for (const key in this.eventMap) {
                if (this.eventMap.hasOwnProperty(key)) {
                    const element = this.eventMap[key];
                    this.dispatcher.off(element.eventName, element.func, element.target);
                }
            }
            this.eventMap = {}
        }

    }
}
